package lvbu.wang.lvbuoperator.utils;

import android.content.Context;
import android.support.annotation.Nullable;

import lvbu.wang.lvbuoperator.core.App;

/**
 * <p>describe：转换相关
 * <p>    note：
 * <p>    note：basic data cov... / sp dp px cov... / code cov...
 * <p>    note：memorySize2Byte / byteSize2MemorySize / byteSize2FitMemorySize
 * <p>    note：timeSpan2Millis / millis2TimeSpan  / millis2FitTimeSpan
 * <p>    note：inputStream outputStream bytes string cov...
 * <p>    note：bytes bitmap drawable view cov...
 * <p>    note：getDistance
 * <p>    note：getTransitionOj
 * <p>    note：
 * <p>  author：zwp on 2017/9/21 mail：1101558280@qq.com web: http://www.zwping.win </p>
 */
public final class ConversionUtil{

    //<editor-fold desc="DP PX SP相互转换">

    /**
     * Convert dp to px by the density of phone
     * <p> translation：通过手机密度将dip 转换 为px
     */
    public static int dp2px(float dp) {
        return (int) (dipToPx(dp) + 0.5f);
    }

    public static float dipToPx(float dp) {
        float scale = App.getInstance().getResources().getDisplayMetrics().density;
        return dp * scale;
    }

    public static int px2dp(float px) {
        return (int) (pxToDip(px) + 0.5f);
    }

    public static int px2dp(Context context, float px) {
        return (int) (pxToDip(context, px) + 0.5f);
    }

    public static float pxToDip(float px) {
        float scale = App.getInstance().getResources().getDisplayMetrics().density;
        return px / scale;
    }

    public static float pxToDip(Context context, float px) {
        float scale = context.getResources().getDisplayMetrics().density;
        return px / scale;
    }

    public static int px2sp(float px) {
        return (int) (pxToSp(px) + 0.5f);
    }

    public static float pxToSp(float px) {
        float fontScale = App.getInstance().getResources().getDisplayMetrics().scaledDensity;
        return px / fontScale;
    }

    public static int sp2px(float sp) {
        return (int) (spToPx(sp) + 0.5f);
    }

    private static float spToPx(float sp) {
        float fontScale = App.getInstance().getResources().getDisplayMetrics().scaledDensity;
        return sp * fontScale;
    }

    /*** xml预览 ***/

    public static int dp2px(@Nullable Context context, float dp) {
        if(context == null) return 0;
        return (int) (dipToPx(context, dp) + 0.5f);
    }

    public static float dipToPx(Context context, float dp) {
        float scale = context.getResources().getDisplayMetrics().density;
        return dp * scale;
    }

    public static int sp2px(Context context, float sp) {
        return (int) (spToPx(context, sp) + 0.5f);
    }

    private static float spToPx(Context context, float sp) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return sp * fontScale;
    }
    //</editor-fold>
}
